﻿Imports System.ComponentModel.Composition
Imports Pronama.NamaAlert
Imports Pronama.NicoVideo.LiveStreaming
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.ComponentModel
Imports System.Text.RegularExpressions
Imports System.Windows.Media
Imports System.Net

<AddInExport("{3F8B4F49-E709-4850-9D1F-3B71A1C8966D}", "ニコ生アラート（本家）",
    Description:="ニコ生アラート（本家）APIから情報を取得します。",
    HasContent:=True,
    HasSetting:=False)>
<Export(GetType(ISourceAddIn))>
Public Class NicoLiveAlertSource
    Implements ISourceAddIn

    Private Activated As Boolean

    Private Mail As String
    Private Password As String


#Region "Implementation"
    Public Event Deactivated(ByVal sender As Object, ByVal e As System.EventArgs) Implements IAddIn.Deactivated
    Protected Sub OnDeactivated(ByVal e As EventArgs)
        RaiseEvent Deactivated(Me, e)
    End Sub

    <Import(GetType(PostMessageCallback))>
    Property PostMessage As PostMessageCallback Implements ISourceAddIn.PostMessage

    <Import(GetType(WriteLogCallback))>
    Public Property WriteLog As WriteLogCallback Implements ISourceAddIn.WriteLog

    Private Settings As ISettings
    Private Helper As IHelper

    Public Sub Initialize(ByVal settings As ISettings, ByVal helper As IHelper) Implements IAddIn.Initialize
        Me.Settings = settings
        Me.Helper = helper

        settings.TryGetValue(Of String)("Mail", Me.Mail)
        settings.TryGetValue(Of String)("Password", Me.Password)
    End Sub

    Public Sub Activate() Implements IAddIn.Activate
        Activated = True
        Connect()
    End Sub

    Public Sub Deactivate() Implements IAddIn.Deactivate
        Activated = False
        If AlertClient IsNot Nothing AndAlso AlertClient.Connected Then
            AlertClient.Close()
        End If
        AlertClient = Nothing
        OnDeactivated(EventArgs.Empty)
    End Sub

    Public Function GetContentControl(ByVal message As Message) As System.Windows.Controls.Control Implements IAddIn.GetContentControl
        Return New NicoLiveAlertSourceSettingPage
    End Function

    Public Function GetSettingControl() As System.Windows.Controls.Control Implements IAddIn.GetSettingControl
        Throw New NotSupportedException
    End Function

    Public Function GetSidebarControl() As System.Windows.Controls.Control Implements IAddIn.GetSidebarControl
        Throw New NotSupportedException
    End Function
#End Region

    Private WithEvents AlertClient As LiveAlertClient

    Private Sub Connect()

        AlertClient = New LiveAlertClient
        AlertClient.ConnectAsync(10)

    End Sub

    Private Sub AlertClient_ConnectCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles AlertClient.ConnectCompleted

    End Sub

    Private Sub AlertClient_ConnectedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles AlertClient.ConnectedChanged
        Dim l = New Log
        l.Type = LogType.Information
        If AlertClient.Connected Then
            l.Text = "Connected"
            WriteLog.Invoke(Me, l)
        Else
            l.Text = "Disconnected"
            WriteLog.Invoke(Me, l)
        End If
    End Sub

    Private Sub AlertClient_LiveProgramPreviewStart(ByVal sender As Object, ByVal e As NicoVideo.LiveStreaming.PreviewLiveProgramStartEventArgs) Handles AlertClient.LiveProgramPreviewStart
        If Not e.Error Is Nothing Then
            Exit Sub
        End If


    End Sub

    Private Sub AlertClient_LiveProgramStarted(ByVal sender As Object, ByVal e As NicoVideo.LiveStreaming.LiveProgramStartedEventArgs) Handles AlertClient.LiveProgramStarted
        If Not e.Error Is Nothing Then
            Exit Sub
        End If

        Dim m = New Message With {.LiveProgram = e.LiveProgram}
        PostMessage.Invoke(Me, m)

    End Sub


End Class
